package com.opennetexam.manager.mapper;

import com.opennetexam.manager.databasemodel.ExamPhoto;
import com.opennetexam.manager.dto.ExaminerPhotoDTO;
import com.opennetexam.manager.sqlprovider.ExamPhotoProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author 奶茶
 * Created on 2017/11/16.
 */
@Mapper
public interface ExamPhotoMapper {


    @SelectProvider(type = ExamPhotoProvider.class,method = "getPhotoList")
    List<Map>  getPhotoList(@Param("planId") String planId, @Param("roomId") String roomId, @Param("sceneId") String sceneId, @Param("examineeName") String examineeName, @Param("examCardNo") String examCardNo, @Param("appId") String appId, @Param("statusId") Integer statusId);

    @Select("select * from ExamPhoto where ExamineeArrangeID = #{examinerArrangeID}")
    List<ExamPhoto> findExamPhotoListByExaminerArrangeID(@Param(value = "examinerArrangeID") String examinerArrangeID);

    @Select("SELECT\n" +
            "\tarrange.ExamineeName,\n" +
            "\tarrange.ExamineeID,\n" +
            "\texamSubject.SubjectName,\n" +
            "\tphoto.Score,\n" +
            "\tphoto.PhotoResourceType,\n" +
            "\tphoto.PhotoResourcePath,\n" +
            "\tIFNULL(examinee.PhotoResource,'') examinerHeadPhoto,\n" +
            "\tphoto.InsertTime\n" +
            "FROM\n" +
            "\tExamPhoto photo\n" +
            "JOIN ExamineeArrange arrange ON photo.ExamineeArrangeID = arrange.ExamineeArrangeID\n" +
            "JOIN ExamSubject examSubject ON examSubject.ExamSubjectID = arrange.ExamSubjectID\n" +
            "RIGHT JOIN Examinee examinee ON examinee.ExamineeID = arrange.ExamineeID\n" +
            "WHERE\n" +
            "\tphoto.ExamineeArrangeID = #{examinerArrangeID} GROUP BY PhotoResourcePath")
    List<ExaminerPhotoDTO> getExaminerPhotoByType(@Param("examinerArrangeID") String examinerArrangeID);

    @Select("SELECT\n" +
            "\tarrange.ExamineeName,\n" +
            "\tarrange.ExamineeID,\n" +
            "\texamSubject.SubjectName,\n" +
            "\tIFNULL(examinee.PhotoResource, '') examinerHeadPhoto\n" +
            "FROM\n" +
            "\tExamineeArrange arrange\n" +
            "JOIN ExamSubject examSubject ON examSubject.ExamSubjectID = arrange.ExamSubjectID\n" +
            "RIGHT JOIN Examinee examinee ON examinee.ExamineeID = arrange.ExamineeID\n" +
            "WHERE\n" +
            "\tarrange.ExamineeArrangeID = #{examinerArrangeID}\n" +
            "LIMIT 1")
    ExaminerPhotoDTO getPhotoHeader(@Param("examinerArrangeID") String examinerArrangeID);

    @Insert("INSERT INTO ExamPhoto (ExamPhotoID,ExamineeArrangeID,PhotoResourceType,Score,FaceCompareResult,PhotoResourcePath,InsertTime) VALUE (#{examPhotoID},#{examineeArrangeID},#{photoResourceType},#{score},#{faceCompareResult},#{photoResourcePath},#{insertTime})")
    Integer saveExamineePhoto(ExamPhoto examPhoto);

    @Select("select * from ExamPhoto where ExamineeArrangeID = #{examineeArrangeID} and PhotoResourceType = #{photoResourceType} order by InsertTime desc")
    List<ExamPhoto> getExamPhotoByWhere(@Param("examineeArrangeID") String examineeArrangeID,@Param("photoResourceType") Integer photoResourceType);

    @Update("UPDATE ExamPhoto\n" +
            " SET `ExamineeArrangeID` = #{examineeArrangeID},\n" +
            " `PhotoResourceType` = #{photoResourceType},\n" +
            " `Score` = #{score},\n" +
            " `FaceCompareResult` = #{faceCompareResult},\n" +
            " `PhotoResourcePath` = #{photoResourcePath},\n" +
            " `InsertTime` = #{insertTime}\n" +
            " WHERE\n" +
            "\t(\n" +
            "\t\t`ExamPhotoID` = #{examPhotoID}\n" +
            "\t)")
    Integer updateExamineePhoto(ExamPhoto examPhoto);
}
